﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;

namespace WcfServer.Classes.Utility
{
    public class KeyHandler
    {
        public static int RSAKeyLength = 4096;
        public static byte[] generateAESKey()
        {
            // AES KEY GENERATION
            AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
            aes.KeySize = 128;
            aes.GenerateKey();
            return aes.Key;
        }
        // Private==true => export private parameters. Generate private first, then use extractPublicFromPrivate to get public of the same parameters.
        public static RSAParameters generateParameters(bool Private)
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(RSAKeyLength);
            return RSA.ExportParameters(Private);
        }

        public static RSAParameters extractPublicFromPrivate(RSAParameters PrivateKey)
        {
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(RSAKeyLength);
            RSA.ImportParameters(PrivateKey);
            return RSA.ExportParameters(false);
        }

        public static RSAParameters getParamFromContainer(String ContainerName, bool Private)
        {
            CspParameters cp = new CspParameters();
            cp.KeyContainerName = ContainerName;

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(RSAKeyLength, cp);
            return rsa.ExportParameters(Private);
        }

        public static String getKey_XML(String ContainerName, bool Private)//It's same as genKey!
        {
            CspParameters cp = new CspParameters();
            cp.KeyContainerName = ContainerName;

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(RSAKeyLength, cp);
            return rsa.ToXmlString(Private);
        }

        /* Key Generation and Storage */
        public static void genKey_SaveInContainer(string ContainerName)
        {
            CspParameters cp = new CspParameters();
            cp.KeyContainerName = ContainerName;

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(RSAKeyLength, cp);
        }

        public static void deleteKeyFromContainer(string ContainerName)
        {
            CspParameters cp = new CspParameters();
            cp.KeyContainerName = ContainerName;

            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(RSAKeyLength, cp);

            rsa.PersistKeyInCsp = false;
            rsa.Clear();
        }

        public static void savePublicXML(string userName /*used as file name */ , string publicXML)
        {
            System.IO.File.WriteAllText(@"Public Keys\" + userName + ".xml", publicXML);
            //CspParameters cp = new CspParameters();
            //cp.KeyContainerName = userName;
            //RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(RSAKeyLength, cp);
            //RSA.FromXmlString(publicXML);
        }

        public static string readPublicXML(string userName)//TODO: check for file availability to prevent exeption
        {
            return System.IO.File.ReadAllText(@"Public Keys\" + userName + ".xml");
        }
    
    }
}
